package com.mbc.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mbc.dto.ConditionDTO;
import com.mbc.dto.MbcBlackListDTO;
import com.mbc.dto.MbcBlackListInfoDTO;
import com.mbc.mapper.BlackListInfoMapper;
import com.mbc.mapper.BlackListMapper;
import com.mbc.service.BlackListService;
import com.mbc.vo.MbcBlackListInfoAndMbcBlackListVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 黑名单服务 impl
 *
 * @author suimeng
 * @date 2024/04/15
 */
@Slf4j

@Service
public class BlackListServiceImpl implements BlackListService {

    @Autowired
    private BlackListMapper blackListMapper;

    @Autowired
    private BlackListInfoMapper blackListInfoMapper;

    /**
     * 插入黑名单用户
     *
     * @param userId          用户 ID
     * @param mbcBlackListDTO MBC黑名单
     * @return boolean
     */
    @Override
    public boolean insertBlackUser(Integer userId, MbcBlackListDTO mbcBlackListDTO) {
        Integer reasonsId = mbcBlackListDTO.getReasonsId();
        String reasonsForReporting = mbcBlackListDTO.getReasonsForReporting();
        String reportUrl = mbcBlackListDTO.getReportUrl();
        String name = mbcBlackListDTO.getName();
        String wechatNumber = mbcBlackListDTO.getWechatNumber();
        String phoneNumber = mbcBlackListDTO.getPhoneNumber();
        LocalDateTime now = LocalDateTime.now();
        return blackListMapper.insertBlackUser(userId, reasonsId, reasonsForReporting, reportUrl, name, wechatNumber,
                phoneNumber, now) > 0;
    }

    /**
     * 新增举报人详细信息
     *
     * @param userId              用户 ID
     * @param mbcBlackListInfoDTO MBC黑名单信息
     * @return boolean
     */
    @Override
    public boolean insertInfoBlackUser(Integer userId, MbcBlackListInfoDTO mbcBlackListInfoDTO) {
        //根据当前用户获取被举报人id
        Integer reasonsId = blackListMapper.getByUserId(userId);
        //用当前举报人id去被举报人详细信息表查询没有这个id值就新增
        log.info("{}", blackListInfoMapper.getByUserId(reasonsId));
        if (blackListInfoMapper.getByUserId(reasonsId) <= 0) {
            return blackListInfoMapper.insertInfoBlackUser(reasonsId, mbcBlackListInfoDTO) > 0;
        }
        return false;
    }

    /**
     * 分页查询全部黑名单用户
     *
     * @param size 大小
     * @param page 页
     * @return {@link List}<{@link MbcBlackListInfoAndMbcBlackListVO}>
     */
    @Override
    public PageInfo<MbcBlackListInfoAndMbcBlackListVO> getByBlackUser(Integer size, Integer page) {
        PageHelper.startPage(size,page);

        List<MbcBlackListInfoAndMbcBlackListVO> list = blackListMapper.getByBlackUser();
        return new PageInfo<>(list);
    }


    /**
     * 条件查询根据查询全部黑名单信息
     *
     * @param conditionDTO 条件 DTO
     * @param size         大小
     * @param page         页
     * @return {@link ArrayList}<{@link MbcBlackListInfoAndMbcBlackListVO}>
     */
    @Override
    public ArrayList<MbcBlackListInfoAndMbcBlackListVO> getByConditionUserIdBlackUser(ConditionDTO conditionDTO, Integer size,
                                                                                      Integer page) {
        Integer offset = (page - 1) * size;
        Integer limit = size;
        Integer userId = conditionDTO.getUserId();
        String name = conditionDTO.getName();
        String nickname = conditionDTO.getNickname();
        ArrayList<MbcBlackListInfoAndMbcBlackListVO> list = new ArrayList<>();
        if (userId != null){
            list  = blackListMapper.getByConditionUserIdBlackUser(userId,offset,limit);
        }else if (name != null){
             list  = blackListMapper.getByConditionNameBlackUser(name,offset,limit);
        }else if (nickname != null){
            list = blackListMapper.getByConditionNickNameBlackUser(nickname,offset,limit);
        }
        return list;
    }

    /**
     * 更新黑名单信息
     *
     * @param userId          用户 ID
     * @param mbcBlackListDTO MBC 黑名单 DTO
     * @return boolean
     */
    @Override
    public boolean updateBlackUser(Integer userId, MbcBlackListDTO mbcBlackListDTO) {
        String reasonsForReporting = mbcBlackListDTO.getReasonsForReporting();
        String reportUrl = mbcBlackListDTO.getReportUrl();
        String wechatNumber = mbcBlackListDTO.getWechatNumber();
        String phoneNumber = mbcBlackListDTO.getPhoneNumber();
        LocalDateTime now = LocalDateTime.now();
        String name = mbcBlackListDTO.getName();
        return blackListMapper.updateBlackUser(userId,reasonsForReporting,reportUrl,wechatNumber,phoneNumber,now,name) > 0;

    }

    /**
     * 更新被举报人信息
     *
     * @param mbcBlackListInfoDTO MBC黑名单信息DTO
     * @param reasonsId         被举报人 ID
     * @return boolean
     */
    @Override
    public boolean updateBlackInfoUser(Integer reasonsId, MbcBlackListInfoDTO mbcBlackListInfoDTO) {
        String sex = mbcBlackListInfoDTO.getSex();
        String name = mbcBlackListInfoDTO.getName();
        String nickname = mbcBlackListInfoDTO.getNickname();
        String phoneNumber = mbcBlackListInfoDTO.getPhoneNumber();
        String wechatNumber = mbcBlackListInfoDTO.getWechatNumber();
        String city = mbcBlackListInfoDTO.getCity();
        String address = mbcBlackListInfoDTO.getAddress();
        return blackListInfoMapper.updateBlackInfoUser(reasonsId,sex,name,nickname,phoneNumber,wechatNumber,city,address) > 0;
    }


}
